package com.ja.openchat.ui;

import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentNameAuthenBinding;
import com.ja.openchat.viewmodels.NameAuthenViewModel;

import me.goldze.mvvmhabit.base.BaseFragment;

//实名认证界面
public class NameAuthenFragment extends BaseFragment<FragmentNameAuthenBinding, NameAuthenViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_name_authen;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();
        BarUtils.setStatusBarLightMode(getActivity(), false);
        BarUtils.setStatusBarColor(getActivity(), getResources().getColor(R.color.color_f2f9f9));
        binding.etName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6), cheseInputFilter});
        binding.tvConfirm.setOnClickListener(view -> {
            viewModel.submitData(binding.etName.getText().toString(), binding.etNumber.getText().toString());
        });
    }

    InputFilter cheseInputFilter = (source, start, end, dest, dstart, dend) -> {
        for (int i = start; i < end; i++) {
            if (!isChinese(source.charAt(i))) {
                return "";
            }
        }
        return null;
    };

    private boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT;
    }
}